import time
import json
import requests
from Config.config import getConfig

headers = eval(getConfig("admin", "cookie"))

# 个人赛
from pom.base import logger


def personal(response):
    user = response['participants']
    return user

# 团队赛
def team(response):
    team_list = response['teams']
    lens = len(team_list)
    user = []
    for i in range(0, lens):
        mem_num = len(team_list[i]['members'])
        for j in range(mem_num):
            user_info = {
                'username':team_list[i]['members'][j]['username'],
                '_id':team_list[i]['members'][j]['_id']
                         }
            user.append(user_info)
    return user


# 更新用户信息
def update_user_info(user, name, update_data):

    update_url = getConfig("base_url", "training_url")+"/usr/updateUser"
    while True:
        response = requests.put(update_url, data=update_data, headers=headers)
        if response.status_code == 200:
            break
        elif response.status_code == 429:
            time.sleep(20)
        else:
            raise Exception(response.text)



def users_to_json(users):
    with open("common/user.json", 'w+') as user_json:
        num = 0
        data={}
        x = 0
        for i in users:
            username = i['username']

            find_user_url = getConfig("base_url", "training_url") + "/usr/list?text={}".format(username)
            while True:
                response= requests.get(find_user_url, headers=headers)
                if response.status_code == 200:
                    break
            response_json = response.json()
            update_data = {
                'user': i['_id'],
                'name': username,
                'pass': '11111111',
                "department": '深圳市公安局什么分局'
            }
            if x % 12 == 0:
                update_data['department'] = '上海市公安局什么分局'
            elif x % 11 == 0:
                update_data['department'] = '武汉市公安局什么分局'
            elif x % 10 == 0:
                update_data['department'] = '西安市公安局什么分局'
            elif x % 9 == 0:
                update_data['department'] = '杭州市公安局什么分局'
            elif x % 8 == 0:
                update_data['department'] = '苏州市公安局什么分局'
            elif x % 7 == 0:
                update_data['department'] = '南通市公安局什么分局'
            elif x % 6 == 0:
                update_data['department'] = '北京市公安局什么分局'
            elif x % 5 == 0:
                update_data['department'] = '三亚市公安局什么分局'
            elif x % 4 == 0:
                update_data['department'] = '广州市公安局什么分局'
            elif x % 3 == 0:
                update_data['department'] = '深圳市公安局什么分局'
            elif x % 2 == 0:
                update_data['department'] = '南京市公安局什么分局'
            for j in response_json['users']:
                id = j['_id']
                if id == i['_id']:
                    num += 1
                    tmp = {"name": "{}".format(j['email']), "passwd": "11111111"}
                    print(update_data)
                    # update_user_info(i, j['email'], update_data)
                    data[str(num)]=tmp
                    # print(tmp)
                    break
            x+=1
        json.dump(data, user_json)


"""
将参赛用户信息输出到json文件中
"""
def user_info(com_id, com_type):
    user_url = getConfig("base_url", "training_url") + "/competition/info?_id={}".format(com_id)

    user_response_json = requests.get(user_url, headers=headers).json()
    users = ""
    if com_type == "personal":
        users = personal(user_response_json)
        logger.info(users)
    elif com_type == "team":
        users = team(user_response_json)
        logger.info(users)
    else:
        logger.error("竞赛类型应为 personal 或者 team")
        raise Exception("竞赛类型应为 personal 或者 team")
    users_to_json(users)
    
user_info("619dd1658a190ab3d516505c", "personal")